package net.andersand.games.yatzy;

import javax.swing.ImageIcon;

class DieIcon extends ImageIcon {

	/** */
	private static final long serialVersionUID = 1;

	private static final String LOCKED = "L";
	private static final String UNLOCKED = "U";

	private String filename;

	/**
	 * Creates a DieIcon with the given value and locking status
	 * 
	 * @param value
	 * @param locked
	 */
	DieIcon(int value, boolean locked) {
		this(createFilename(value, locked));
	}

	/**
	 * Creates a die icon with the given filename.
	 * 
	 * @param filename
	 */
	private DieIcon(String filename) {
		super(Resources.get(filename));
		this.filename = filename;
	}

	String getFilename() {
		return filename;
	}

	boolean isLockedDieIcon() {
		return filename.contains("locked");
	}

	static private String createFilename(int value, boolean locked) {
		if (value < 0) {
			value = 0;
		}
		return YatzyFrame.IMAGES + value + (locked ? LOCKED : UNLOCKED) + ".gif";
	}

	private String getToggleLockedFilename() {
		String fn = filename.replaceFirst(LOCKED, "");
		if (!isLockedDieIcon()) {
			fn = fn.replaceFirst("\\.gif", LOCKED + ".gif");
		}
		return fn;
	}

	DieIcon getToggleLockedDieIcon() {
		String fn = getToggleLockedFilename();
		return new DieIcon(fn);
	}

}
